import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.hkust.Camera;
import ij.hkust.Constants;
import ij.hkust.gui.laserCtr.LaserCtrChooser;
import ij.hkust.gui.working.ImgProcessor;
import ij.hkust.gui.working.WorkingWin;
import ij.io.FileSaver;
import ij.plugin.PlugIn;
import ij.plugin.frame.RoiManager;
import ij.process.ByteProcessor;
import ij.process.ImageProcessor;
import ij.process.ShortProcessor;

import java.io.File;

import mmcorej.CMMCore;

import org.micromanager.MMStudioMainFrame;

public class CameraSequence implements PlugIn {
	public static CMMCore core = null;
	ImageProcessor ip;
	Object img;
	ImagePlus imp;
	ImageStack res;
	RoiManager rmanager;

	public boolean loadCamera() {
		try {

			MMStudioMainFrame gui = MMStudioMainFrame.getInstance();
			core = gui.getCore();
			if(gui.isLiveModeOn()){
//				IJ.showMessage("please stop the live mode!");
				gui.enableLiveMode(false);
//				core.stop(null);
//				ImgProcessor.isRun = false;
//				LaserCtrChooser.imgProcessor = null;
//				return false;
			}
		} catch (Exception e) {
			IJ.showMessage("error please close the live window before use this software.");
		}
		return true;
	}

	@Override
	public void run(String arg0) {
		IJ.register(CameraSequence.class); // static method of ij/IJ
//		if (core == null) {
			if (!loadCamera()) {
				return;
			}
//		}

		try {
			int owidth = (int) core.getImageWidth();
			int oheight = (int) core.getImageHeight();
			Constants.imgWidth = owidth;
			Constants.imgHeight = oheight;
			int index = 0;

			int count = 0;
//			IJ.log("ca start...");
			core.startContinuousSequenceAcquisition(0.0);
			while (count < Camera.imageCount) {
				ip = null;
				//this is for 
				if(!Constants.isCanStart){
					Thread.currentThread().sleep(500);
					continue;
				}
//				IJ.log("start take image...");
				if (core.getRemainingImageCount() > 0) {
					img = core.popNextImage();
//					IJ.log("get image");

					if (img instanceof byte[]) {
						ip = new ByteProcessor(owidth, oheight);
						ip.setPixels((byte[]) img);
					} else if (img instanceof short[]) {
						ip = new ShortProcessor(owidth, oheight);
						ip.setPixels((short[]) img);
					} else {
						IJ.error("Image Type Error");
						System.exit(-1);
					}
				} else
					continue;
//				IJ.log("ca converted image");
				ImageProcessor ip2 = ip.duplicate();

				ImagePlus imp = new ImagePlus("" + index, ip2);
				FileSaver fs = new FileSaver(imp);
				File file = new File(Constants.fileSavePath);
				if (!file.exists()) {
					file.mkdirs();
				}
//				IJ.log("ca save image");
				Constants.addRawImage(img, 1);
				fs.saveAsTiff(Constants.fileSavePath + count + ".tif");
//				IJ.log("ca update working win");
				WorkingWin.update(1, Constants.fileSavePath + count + ".tif");
//				WorkingWin.update(1, imp.duplicate());
				index++;
				count++;
			}
			core.stopSequenceAcquisition();
//			IJ.log("############stop camera");
		} catch (Exception e) {
			IJ.error(e.getMessage());
			System.exit(-1);
		}
	}

}
